]> git.saurik.com Git - bison.git/blame - tests/c++.at
tests: update our Valgrind suppression files
[bison.git] / tests / c++.at
CommitLineData
535ee0cb 1# Checking the C++ Features. -*- Autotest -*-
7d424de1 2
3209eb1c 3# Copyright (C) 2004-2005, 2007-2015 Free Software Foundation, Inc.
e019c247 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
e019c247 6# it under the terms of the GNU General Public License as published by
f16b0819
PE
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
e019c247
AD
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
f16b0819 14#
e019c247 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
e019c247
AD
17
18AT_BANNER([[C++ Features.]])
19
20
75ae8299
AD
21## --------------- ##
22## C++ Locations. ##
23## --------------- ##
24
25AT_SETUP([C++ Locations])
26
27AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "lalr1.cc"])
28AT_DATA_GRAMMAR([[input.y]],
29[[%code {#include <sstream>}
30%locations
31%debug
32%skeleton "lalr1.cc"
33%code
34{
35]AT_YYERROR_DECLARE[
36]AT_YYLEX_DECLARE[
37}
38%%
39exp: %empty;
40%%
41]AT_YYERROR_DEFINE[
42]AT_YYLEX_DEFINE[
43
44template <typename T>
45bool
46check (const T& in, const std::string& s)
47{
48 std::stringstream os;
49 os << in;
50 if (os.str () != s)
51 {
52 std::cerr << "fail: " << os.str () << ", expected: " << s << std::endl;
53 return false;
54 }
55 return true;
56}
57
58int
59main (void)
60{
61 int fail = 0;
62 ]AT_YYLTYPE[ loc; fail += check (loc, "1.1");
56351d4c 63 fail += check (loc + 10, "1.1-10");
75ae8299
AD
64 loc += 10; fail += check (loc, "1.1-10");
65 loc += -5; fail += check (loc, "1.1-5");
56351d4c 66 fail += check (loc - 5, "1.1");
75ae8299
AD
67 loc -= 5; fail += check (loc, "1.1");
68 // Check that we don't go below.
69 // http://lists.gnu.org/archive/html/bug-bison/2013-02/msg00000.html
70 loc -= 10; fail += check (loc, "1.1");
71
72 loc.columns (10); loc.lines (10); fail += check (loc, "1.1-11.0");
73 loc.lines (-2); fail += check (loc, "1.1-9.0");
74 loc.lines (-10); fail += check (loc, "1.1");
56351d4c
AD
75
76 ]AT_YYLTYPE[ loc2 (YY_NULLPTR, 5, 10);
77 fail += check (loc2, "5.10");
78 fail += check (loc + loc2, "1.1-5.9");
79 loc += loc2; fail += check (loc, "1.1-5.9");
75ae8299
AD
80 return !fail;
81}
82]])
83
84AT_FULL_COMPILE([input])
85AT_PARSER_CHECK([./input], 0)
86AT_BISON_OPTION_POPDEFS
87AT_CLEANUP
88
89
97ae878e
AD
90## --------------------------- ##
91## C++ Variant-based Symbols. ##
92## --------------------------- ##
93
94AT_SETUP([C++ Variant-based Symbols])
95
96AT_KEYWORDS([variant])
97
98AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" %debug $1])
95241611 99# Store strings and integers in a vector of strings.
f2e1d409 100AT_DATA_GRAMMAR([list.y],
97ae878e
AD
101[[%skeleton "lalr1.cc"
102%define api.value.type variant
103%define parse.assert
104%debug
105
106%code top
107{
108 // Get access to stack_symbol_type for the tests.
109# define private public
110}
111%code provides
112{
113 ]AT_YYLEX_DECLARE[
114}
115
116%token <int> INT "int"
95241611 117%type < std::vector<int> > exp
97ae878e
AD
118
119%printer { yyo << $$; } <int>
120%printer
121 {
95241611 122 for (std::vector<int>::const_iterator i = $$.begin (); i != $$.end (); ++i)
97ae878e
AD
123 {
124 if (i != $$.begin ())
125 yyo << ", ";
126 yyo << *i;
127 }
95241611 128 } < std::vector<int> >
97ae878e 129
95241611 130%code requires { #include <vector> }
97ae878e
AD
131%code { int yylex (yy::parser::semantic_type* yylval); }
132
133%%
134exp: "int" { $$.push_back ($1); }
135%%
136]AT_YYERROR_DEFINE[
137]AT_YYLEX_DEFINE[
138
139int main()
140{
141 {
142 yy::parser::symbol_type s = yy::parser::make_INT(12);
143 std::cerr << s.value.as<int>() << std::endl;
144 }
145
146 {
147 yy::parser::symbol_type s = yy::parser::make_INT(123);
148 yy::parser::stack_symbol_type ss(1, s);
149 std::cerr << ss.value.as<int>() << std::endl;
150 }
151
152 {
153 yy::parser::stack_type st;
154 for (int i = 0; i < 100; ++i)
155 {
156 yy::parser::symbol_type s(yy::parser::make_INT(i));
157 yy::parser::stack_symbol_type ss(1, s);
158 st.push(ss);
159 }
160 }
161}
162]])
163
f2e1d409 164AT_FULL_COMPILE([list])
97ae878e
AD
165AT_PARSER_CHECK([./list], 0, [],
166[12
167123
168])
169
170AT_BISON_OPTION_POPDEFS
171AT_CLEANUP
172
173
76307410
AD
174## ---------- ##
175## Variants. ##
176## ---------- ##
177
00cebd11
AD
178# Check that the variants are properly supported, including in error
179# recovery.
180
e36ec1f4
AD
181# AT_TEST([DIRECTIVES])
182# ---------------------
76307410 183# Check the support of variants in C++, with the additional DIRECTIVES.
e36ec1f4 184m4_pushdef([AT_TEST],
76307410
AD
185[AT_SETUP([Variants $1])
186
db40c3f8 187AT_BISON_OPTION_PUSHDEFS([%debug $1])
95241611 188# Store strings and integers in a vector of strings.
f2e1d409 189AT_DATA_GRAMMAR([list.y],
76307410 190[[%debug
bc603897 191%define api.value.type variant
e5eb92e7
AD
192]m4_bpatsubst([$1], [\\n], [
193])[
76307410
AD
194
195%code requires // code for the .hh file
196{
95241611 197#include <vector>
76307410 198#include <string>
95241611 199typedef std::vector<std::string> strings_type;
76307410
AD
200}
201
95241611 202%code top // code for the .cc file.
76307410 203{
f5fceda5 204#include <cstdlib> // abort, getenv
76307410 205#include <iostream>
95241611 206#include <vector>
76307410 207#include <sstream>
95241611 208#include <string>
76307410 209
76307410 210
95241611
AD
211 typedef std::vector<std::string> strings_type;
212
213 namespace yy
76307410 214 {
95241611 215 // Must be available early, as is used in %destructor.
76307410
AD
216 std::ostream&
217 operator<<(std::ostream& o, const strings_type& s)
218 {
f5da8149 219 o << '(';
77a1a208 220 for (strings_type::const_iterator i = s.begin (); i != s.end (); ++i)
f5da8149
AD
221 {
222 if (i != s.begin ())
223 o << ", ";
224 o << *i;
225 }
226 return o << ')';
76307410
AD
227 }
228 }
95241611 229}
76307410 230
95241611
AD
231%code // code for the .cc file.
232{
233 namespace yy
76307410 234 {
95241611
AD
235 static]AT_TOKEN_CTOR_IF([[
236 parser::symbol_type yylex ()]], [[
237 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
238 parser::location_type* yylloc])[)]])[;
239
240 // Conversion to string.
241 template <typename T>
242 inline
243 std::string
244 to_string (const T& t)
245 {
246 std::ostringstream o;
247 o << t;
248 return o.str ();
249 }
76307410
AD
250 }
251}
252
f5da8149 253%token <::std::string> TEXT;
76307410 254%token <int> NUMBER;
76307410 255%token END_OF_FILE 0;
00cebd11 256%token COMMA ","
76307410 257
f5da8149 258%type <::std::string> item;
cb823b6f
AD
259// Using the template type to exercize its parsing.
260// Starting with :: to ensure we don't output "<::" which starts by the
261// digraph for the left square bracket.
95241611 262%type <::std::vector<std::string>> list;
76307410 263
16bb9f16 264%printer { yyo << $$; }
95241611 265 <int> <::std::string> <::std::vector<std::string>>;
ee028dce
AD
266%destructor { std::cerr << "Destroy: " << $$ << '\n'; } <*>;
267%destructor { std::cerr << "Destroy: \"" << $$ << "\"\n"; } <::std::string>;
76307410
AD
268%%
269
270result:
f5da8149 271 list { std::cout << $][1 << std::endl; }
76307410
AD
272;
273
274list:
00cebd11
AD
275 item { $$.push_back ($][1); }
276| list "," item { std::swap ($$, $][1); $$.push_back ($][3); }
277| list error { std::swap ($$, $][1); }
76307410
AD
278;
279
280item:
00cebd11 281 TEXT { std::swap ($$, $][1); }
8a4ec359 282| NUMBER { if ($][1 == 3) YYERROR; else $$ = to_string ($][1); }
76307410
AD
283;
284%%
26a4d3c8
AD
285]AT_TOKEN_CTOR_IF([],
286[[#ifdef TWO_STAGE_BUILD
e36ec1f4 287# define BUILD(Type, Value) build<Type> () = Value
e5eb92e7 288#else
e36ec1f4 289# define BUILD(Type, Value) build (Value)
e5eb92e7 290#endif
26a4d3c8 291]])[
e36ec1f4
AD
292#define STAGE_MAX 5
293namespace yy
76307410 294{
e36ec1f4
AD
295 static]AT_TOKEN_CTOR_IF([[
296 parser::symbol_type yylex ()]], [[
f5fceda5
AD
297 parser::token_type yylex (parser::semantic_type* yylval]AT_LOCATION_IF([,
298 parser::location_type* yylloc])[)]])[
299 {]AT_LOCATION_IF([
300 typedef parser::location_type location;])[
00cebd11
AD
301 // The 5 is a syntax error whose recovery requires that we discard
302 // the lookahead. This tests a regression, see
303 // <http://savannah.gnu.org/support/?108481>.
304 static char const *input = "0,1,2,3,45,6";
305 switch (int stage = *input++)
306 {
307 case 0:]AT_TOKEN_CTOR_IF([[
f5fceda5
AD
308 return parser::make_END_OF_FILE (]AT_LOCATION_IF([location ()])[);]],
309[AT_LOCATION_IF([
310 *yylloc = location ();])[
e36ec1f4 311 return parser::token::END_OF_FILE;]])[
00cebd11
AD
312
313 case ',':
314 ]AT_TOKEN_CTOR_IF([[
315 return parser::make_COMMA (]AT_LOCATION_IF([location ()])[);]], [[
316]AT_LOCATION_IF([
f5fceda5 317 *yylloc = location ();])[
00cebd11
AD
318 return parser::token::COMMA;]])[
319
320 default:
321 stage = stage - '0';
322 if (stage % 2)
323 {]AT_TOKEN_CTOR_IF([[
324 return parser::make_NUMBER (stage]AT_LOCATION_IF([, location ()])[);]], [[
325 yylval->BUILD (int, stage);]AT_LOCATION_IF([
326 *yylloc = location ();])[
327 return parser::token::NUMBER;]])[
328 }
329 else
330 {]AT_TOKEN_CTOR_IF([[
331 return parser::make_TEXT (to_string (stage)]AT_LOCATION_IF([, location ()])[);]], [[
332 yylval->BUILD (std::string, to_string (stage));]AT_LOCATION_IF([
333 *yylloc = location ();])[
334 return parser::token::TEXT;]])[
335 }
336 }
337
e36ec1f4
AD
338 abort ();
339 }
76307410
AD
340}
341
f5fceda5 342]AT_YYERROR_DEFINE[
3ef9fa8f 343]AT_MAIN_DEFINE[
76307410
AD
344]])
345
f2e1d409 346AT_FULL_COMPILE([list])
2c08dc50 347AT_PARSER_CHECK([./list], 0,
ee028dce
AD
348[[(0, 1, 2, 4, 6)
349]],
350[[Destroy: ""
351Destroy: "0"
ee028dce
AD
352Destroy: 1
353Destroy: "1"
354Destroy: ()
355Destroy: ""
356Destroy: "2"
357Destroy: ()
358Destroy: ""
359Destroy: 3
360Destroy: ()
361Destroy: ""
362Destroy: "4"
363Destroy: ()
364Destroy: ()
365Destroy: 5
366Destroy: ()
367Destroy: ""
368Destroy: "6"
369Destroy: ()
370Destroy: (0, 1, 2, 4, 6)
371]])
76307410 372
e36ec1f4 373AT_BISON_OPTION_POPDEFS
76307410
AD
374AT_CLEANUP
375])
376
db40c3f8
AD
377AT_TEST([[%skeleton "lalr1.cc" ]])
378AT_TEST([[%skeleton "lalr1.cc" %define parse.assert]])
00cebd11 379AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %locations]])
db40c3f8
AD
380AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %code {\n#define TWO_STAGE_BUILD\n}]])
381AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor]])
382AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_}]])
00cebd11 383AT_TEST([[%skeleton "lalr1.cc" %define parse.assert %define api.token.constructor %define api.token.prefix {TOK_} %locations]])
e36ec1f4
AD
384
385m4_popdef([AT_TEST])
76307410
AD
386
387
e019c247
AD
388## ----------------------- ##
389## Doxygen Documentation. ##
390## ----------------------- ##
391
392m4_define([AT_CHECK_DOXYGEN],
393[m4_case([$1],
394 [Public], [m4_pushdef([AT_DOXYGEN_PRIVATE], [NO])],
395 [Private], [m4_pushdef([AT_DOXYGEN_PRIVATE], [YES])],
e9690142 396 [m4_fatal([invalid argument: $1])])
e019c247
AD
397AT_SETUP([Doxygen $1 Documentation])
398
535ee0cb 399AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
e019c247
AD
400AT_DATA([input.yy],
401[[%skeleton "lalr1.cc"
402%locations
e1d06b52 403%defines
e019c247 404%debug
e019c247 405%%
c7442984 406exp: /* empty */;
e019c247 407%%
535ee0cb 408]AT_YYERROR_DEFINE[
e019c247
AD
409]])
410
e1d06b52 411AT_BISON_CHECK([-o input.cc input.yy])
e019c247
AD
412
413AT_DATA([Doxyfile],
414[# The PROJECT_NAME tag is a single word (or a sequence of words
415# surrounded by quotes) that should identify the project.
416PROJECT_NAME = "Bison C++ Parser"
417
418# The QUIET tag can be used to turn on/off the messages that are
419# generated by doxygen. Possible values are YES and NO. If left blank
420# NO is used.
421QUIET = YES
422
423# The WARNINGS tag can be used to turn on/off the warning messages
424# that are generated by doxygen. Possible values are YES and NO. If
425# left blank NO is used.
426WARNINGS = YES
427# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate
428# warnings for undocumented members. If EXTRACT_ALL is set to YES then
429# this flag will automatically be disabled.
430WARN_IF_UNDOCUMENTED = YES
431# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings
432# for potential errors in the documentation, such as not documenting
433# some parameters in a documented function, or documenting parameters
434# that don't exist or using markup commands wrongly.
435WARN_IF_DOC_ERROR = YES
436# The WARN_FORMAT tag determines the format of the warning messages
437# that doxygen can produce. The string should contain the $file,
438# $line, and $text tags, which will be replaced by the file and line
439# number from which the warning originated and the warning text.
440WARN_FORMAT = "$file:$line: $text"
441
442# If the EXTRACT_ALL tag is set to YES doxygen will assume all
443# entities in documentation are documented, even if no documentation
444# was available. Private class members and static file members will
445# be hidden unless the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set
446# to YES
447EXTRACT_ALL = YES
448
449# If the EXTRACT_PRIVATE tag is set to YES all private members of a
450# class will be included in the documentation.
451EXTRACT_PRIVATE = AT_DOXYGEN_PRIVATE
452
453# If the EXTRACT_STATIC tag is set to YES all static members of a file
454# will be included in the documentation.
455EXTRACT_STATIC = AT_DOXYGEN_PRIVATE
456])
457
458AT_CHECK([doxygen --version || exit 77], 0, ignore)
459AT_CHECK([doxygen], 0, [], [ignore])
460
535ee0cb 461AT_BISON_OPTION_POPDEFS
e019c247
AD
462AT_CLEANUP
463
464m4_popdef([AT_DOXYGEN_PRIVATE])
465])# AT_CHECK_DOXYGEN
466
467AT_CHECK_DOXYGEN([Public])
468AT_CHECK_DOXYGEN([Private])
793fbca5 469
76307410 470
793fbca5
JD
471## ------------ ##
472## Namespaces. ##
473## ------------ ##
474
56b91ae0
AD
475# AT_TEST(NAMESPACE-DECL, [COMPILE-ERROR])
476# ----------------------------------------
793fbca5
JD
477# See if Bison can handle %define namespace "NAMESPACE-DECL". If COMPILE-ERROR
478# is specified, then Bison should accept the input, but compilation will fail,
479# so don't check compilation.
56b91ae0 480m4_pushdef([AT_TEST],
eb0e86ac 481[AT_BISON_OPTION_PUSHDEFS([%language "C++" %define api.namespace {$1}])
e1d06b52 482AT_DATA_GRAMMAR([[input.yy]],
793fbca5 483[[%language "C++"
eb0e86ac 484%define api.namespace {]$1[}
793fbca5
JD
485%union { int i; }
486%define global_tokens_and_yystype
2ea7730c 487%locations
793fbca5
JD
488
489%code {
490 // YYSTYPE contains a namespace reference.
d73e55e0 491 int yylex (YYSTYPE *lval, const ]$1[::parser::location_type*) {
793fbca5
JD
492 lval->i = 3;
493 return 0;
494 }
495}
496
497%%
498
499start: ;
500
501%%
502
503void
504]$1[::parser::error (const ]$1[::parser::location_type &loc,
505 const std::string &msg)
506{
507 std::cerr << "At " << loc << ": " << msg << std::endl;
508}
509
56b91ae0 510]AT_MAIN_DEFINE[
793fbca5
JD
511]])
512
bb1c50d8 513
e1d06b52 514AT_BISON_CHECK([[-o input.cc input.yy]])
793fbca5
JD
515
516m4_if([$#], [1],
e1d06b52 517[AT_COMPILE_CXX([[input]])
793fbca5 518AT_PARSER_CHECK([[./input]])])
56b91ae0 519AT_BISON_OPTION_POPDEFS
793fbca5
JD
520])
521
522AT_SETUP([[Relative namespace references]])
56b91ae0
AD
523AT_TEST([[foo]])
524AT_TEST([[foo::bar]])
525AT_TEST([[foo::bar::baz]])
793fbca5
JD
526AT_CLEANUP
527
528AT_SETUP([[Absolute namespace references]])
56b91ae0
AD
529AT_TEST([[::foo]])
530AT_TEST([[::foo::bar]])
531AT_TEST([[::foo::bar::baz]])
bb1c50d8
AD
532AT_TEST([[@tb@::foo]])
533AT_TEST([[ @tb@ ::foo::bar]])
56b91ae0 534AT_TEST([[ ::foo::bar::baz]])
793fbca5
JD
535AT_CLEANUP
536
537AT_SETUP([[Syntactically invalid namespace references]])
56b91ae0
AD
538AT_TEST([[:foo:bar]], [[-]])
539AT_TEST([[foo: :bar]], [[-]])
4c9b8f13
AD
540# This one is interesting because '[3]' is encoded as '@<:@3@:>@', which
541# contains single occurrences of ':'.
56b91ae0
AD
542AT_TEST([[foo[3]::bar::baz]], [[-]])
543AT_TEST([[foo::bar,baz]], [[-]])
544AT_TEST([[foo::bar::(baz /* Pacify Emacs ) */]], [[-]])
793fbca5 545AT_CLEANUP
d59beda0 546
56b91ae0 547m4_popdef([AT_TEST])
d59beda0
JD
548
549## -------------------------------------- ##
550## Syntax error discarding no lookahead. ##
551## -------------------------------------- ##
552
553# After a syntax error, lalr1.cc used to not check whether there
554# actually is a lookahead before discarding the lookahead. As a result,
555# it mistakenly invoked the destructor for the previous lookahead.
556
557AT_SETUP([[Syntax error discarding no lookahead]])
558
3ef9fa8f
AD
559AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
560
e1d06b52 561AT_DATA_GRAMMAR([[input.y]],
d59beda0
JD
562[[%skeleton "lalr1.cc"
563
564%code {
565 #include <string>
a8c5aaa5 566 int yylex (yy::parser::semantic_type *);
d59beda0
JD
567 #define USE(Args)
568}
569
d59beda0
JD
570%define parse.error verbose
571
572%nonassoc 'a' ;
573
574%destructor {
575 std::cerr << "Discarding 'a'." << std::endl;
576} 'a'
577
578%%
579
580start: error-reduce consistent-error 'a' { USE ($3); };
581
582error-reduce:
583 'a' 'a' consistent-error 'a' { USE (($1, $2, $4)); }
584| 'a' error { std::cerr << "Reducing 'a'." << std::endl; USE ($1); }
585;
586
587consistent-error:
588 'a'
589| /*empty*/ %prec 'a'
590;
591
5335b65a
JD
592// Provide another context in which all rules are useful so that this
593// test case looks a little more realistic.
594start: 'b' consistent-error ;
595
d59beda0
JD
596%%
597
598int
a8c5aaa5 599yylex (yy::parser::semantic_type *)
d59beda0
JD
600{
601 static char const *input = "aa";
602 return *input++;
603}
604
605void
a8c5aaa5 606yy::parser::error (const std::string &m)
d59beda0
JD
607{
608 std::cerr << m << std::endl;
609}
610
3ef9fa8f 611]AT_MAIN_DEFINE[
d59beda0 612]])
3ef9fa8f 613
e1d06b52 614AT_FULL_COMPILE([[input]])
d59beda0
JD
615# This used to print "Discarding 'a'." again at the end.
616AT_PARSER_CHECK([[./input]], [[1]], [[]],
617[[syntax error
618Discarding 'a'.
619Reducing 'a'.
620]])
621
3ef9fa8f 622AT_BISON_OPTION_POPDEFS
d59beda0 623AT_CLEANUP
199a2d6d
AD
624
625
626## --------------------------- ##
627## Syntax error as exception. ##
628## --------------------------- ##
629
630AT_SETUP([[Syntax error as exception]])
631
3ef9fa8f
AD
632AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc"])
633
e1d06b52 634AT_DATA_GRAMMAR([[input.y]],
199a2d6d
AD
635[[%skeleton "lalr1.cc"
636
637%code
638{
0bb5783b 639 #include <cstdlib>
199a2d6d
AD
640 int yylex (yy::parser::semantic_type *);
641}
642
bc603897 643%define api.value.type variant
199a2d6d
AD
644%define parse.error verbose
645%define parse.trace
646%%
647
648start:
649 thing
650| start thing
651;
652
653thing:
654 error { std::cerr << "caught error" << std::endl; }
655| item
656;
657
658item:
659 'a'
660| 's'
661 {
77a1a208 662 throw yy::parser::syntax_error ("invalid expression");
199a2d6d
AD
663 }
664
665%%
666
667int
668yylex (yy::parser::semantic_type *)
669{
a6552c5d
AD
670 // 's': syntax error, 'l': lexical error.
671 static char const *input = "asal";
199a2d6d
AD
672 switch (int res = *input++)
673 {
a6552c5d 674 case 'l':
77a1a208 675 throw yy::parser::syntax_error ("invalid character");
199a2d6d
AD
676 default:
677 return res;
678 }
679}
680
681void
682yy::parser::error (const std::string &m)
683{
684 std::cerr << "error: " << m << std::endl;
685}
3ef9fa8f 686]AT_MAIN_DEFINE[
199a2d6d 687]])
3ef9fa8f 688
e1d06b52 689AT_FULL_COMPILE([[input]])
199a2d6d
AD
690
691AT_PARSER_CHECK([[./input]], [[0]], [[]],
692[[error: invalid expression
693caught error
a6552c5d
AD
694error: invalid character
695caught error
199a2d6d
AD
696]])
697
3ef9fa8f 698AT_BISON_OPTION_POPDEFS
199a2d6d 699AT_CLEANUP
cff92661
AD
700
701
702## ------------------ ##
703## Exception safety. ##
704## ------------------ ##
705
5cf6e669
AD
706# AT_TEST([BISON-DIRECTIVES = ''], [WITH-RECOVERY = "with"])
707# ----------------------------------------------------------
dc8e535c 708# Check that no object is leaked when exceptions are thrown.
5cf6e669 709# WITH-RECOVERY = "with" or "without".
dc8e535c 710m4_pushdef([AT_TEST],
5cf6e669 711[AT_SETUP([[Exception safety $2 error recovery $1]])
cff92661 712
02681666
AD
713AT_SKIP_IF_EXCEPTION_SUPPORT_IS_POOR
714
dc8e535c 715AT_BISON_OPTION_PUSHDEFS([%skeleton "lalr1.cc" $1])
cff92661
AD
716
717AT_DATA_GRAMMAR([[input.yy]],
718[[%skeleton "lalr1.cc"
cff92661 719%debug
e8b86af8 720%error-verbose
dc8e535c 721$1
cff92661
AD
722%code requires
723{
e8b86af8 724 #include <cassert>
cff92661
AD
725 #include <cstdlib> // size_t and getenv.
726 #include <iostream>
5cf6e669 727 #include <set>
eaa476a7 728 #include <string>
cff92661 729
e8b86af8 730 bool debug = false;
cff92661 731
5cf6e669 732 /// A class that tracks its instances.
cff92661
AD
733 struct Object
734 {
e39fcdda
AD
735 char val;
736
5cf6e669
AD
737 Object ()
738 : val ('?')
e39fcdda 739 {
e39fcdda 740 log (this, "Object::Object");
5cf6e669 741 Object::instances.insert (this);
e39fcdda
AD
742 }
743
5cf6e669
AD
744 Object (const Object& that)
745 : val (that.val)
e39fcdda 746 {
e39fcdda 747 log (this, "Object::Object");
5cf6e669 748 Object::instances.insert (this);
e39fcdda
AD
749 }
750
5cf6e669
AD
751 Object (char v)
752 : val (v)
e39fcdda 753 {
5cf6e669
AD
754 log (this, "Object::Object");
755 Object::instances.insert (this);
e39fcdda
AD
756 }
757
758 ~Object ()
759 {
e39fcdda 760 log (this, "Object::~Object");
eaa476a7 761 objects::iterator i = instances.find (this);
5cf6e669
AD
762 // Make sure this object is alive.
763 assert (i != instances.end ());
764 Object::instances.erase (i);
765 }
766
767 Object& operator= (char v)
768 {
769 val = v;
770 return *this;
e39fcdda
AD
771 }
772
773 // Static part.
5cf6e669 774 typedef std::set<const Object*> objects;
e8b86af8 775 static objects instances;
cff92661 776
e8b86af8
AD
777 static bool
778 empty ()
779 {
e39fcdda 780 return instances.empty ();
e8b86af8
AD
781 }
782
783 static void
784 log (Object const *o, const std::string& msg)
cff92661 785 {
cff92661 786 if (debug)
e8b86af8
AD
787 {
788 if (o)
789 std::cerr << o << "->";
790 std::cerr << msg << " {";
791 const char* sep = " ";
792 for (objects::const_iterator i = instances.begin(),
793 i_end = instances.end();
794 i != i_end;
795 ++i)
796 {
797 std::cerr << sep << *i;
798 sep = ", ";
799 }
800 std::cerr << " }" << std::endl;
801 }
802 }
cff92661
AD
803 };
804}
805
806%code
807{
808 #include <cassert>
a2642464 809 #include <cstring> // strchr
cff92661
AD
810 #include <stdexcept>
811 int yylex (yy::parser::semantic_type *);
e8b86af8 812 Object::objects Object::instances;
cff92661
AD
813 static char const *input;
814}
815
dc8e535c
AD
816]AT_VARIANT_IF([[
817%printer
cff92661 818{
dc8e535c
AD
819 yyo << &$$ << " '" << $$.val << '\'';
820 if ($$.val == 'p')
821 throw std::runtime_error ("printer");
822} <Object>;
cff92661 823
dc8e535c
AD
824%token <Object> 'a' 'E' 'e' 'p' 'R' 's' 'T'
825%type <Object> list item
826]], [[
827%union
a2642464 828{
dc8e535c 829 Object *obj;
a2642464 830}
cff92661 831%destructor { delete $$; } <obj>;
25a6ad2f
AD
832%printer
833{
e8b86af8 834 yyo << $$ << " '" << $$->val << '\'';
25a6ad2f
AD
835 if ($$->val == 'p')
836 throw std::runtime_error ("printer");
837} <obj>;
cff92661 838
e8b86af8 839%token <obj> 'a' 'E' 'e' 'p' 'R' 's' 'T'
cff92661 840%type <obj> list item
dc8e535c
AD
841]])[
842
843%initial-action
844{
845 if (strchr (input, 'i'))
846 throw std::runtime_error ("initial-action");
847}
cff92661
AD
848
849%%
850
dc8e535c 851start: list {]AT_VARIANT_IF([], [ delete $][1]; )[};
cff92661
AD
852
853list:
16bb9f16 854 item { $$ = $][1; }
dc8e535c 855 // Right recursion to load the stack.
16bb9f16 856| item list { $$ = $][1; ]AT_VARIANT_IF([], [delete $][2]; )[}
cff92661
AD
857;
858
859item:
16bb9f16 860 'a' { $$ = $][1; }
ee028dce 861| 'e' { YYUSE ($$); YYUSE ($][1); error ("syntax error"); }
e8b86af8
AD
862// Not just 'E', otherwise we reduce when 'E' is the lookahead, and
863// then the stack is emptied, defeating the point of the test.
ee028dce 864| 'E' 'a' { YYUSE ($][1); $$ = $][2; }
16bb9f16
AD
865| 'R' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYERROR; }
866| 'p' { $$ = $][1; }
867| 's' { $$ = $][1; throw std::runtime_error ("reduction"); }
868| 'T' { ]AT_VARIANT_IF([], [$$ = YY_NULLPTR; delete $][1]; )[YYABORT; }
5cf6e669
AD
869]m4_if([$2], [with],
870[[| error { $$ = ]AT_VARIANT_IF([], [new ])[Object ('R'); yyerrok; }]])[
e8b86af8 871;
cff92661
AD
872%%
873
874int
875yylex (yy::parser::semantic_type *lvalp)
876{
a2642464 877 // 'a': no error.
e8b86af8
AD
878 // 'e': user action calls error.
879 // 'E': syntax error, with yyerror that throws.
a2642464
AD
880 // 'i': initial action throws.
881 // 'l': yylex throws.
e8b86af8 882 // 'R': call YYERROR in the action
a2642464 883 // 's': reduction throws.
e8b86af8 884 // 'T': call YYABORT in the action
cff92661
AD
885 switch (int res = *input++)
886 {
dc8e535c
AD
887 case 'l':
888 throw std::runtime_error ("yylex");
889 default:
39bace5d
AD
890 lvalp->]AT_VARIANT_IF([build (Object (res))],
891 [obj = new Object (res)])[;
dc8e535c
AD
892 // Fall through.
893 case 0:
894 return res;
cff92661
AD
895 }
896}
897
e8b86af8
AD
898/* A C++ error reporting function. */
899void
23d13411 900yy::parser::error (const std::string& m)
e8b86af8 901{
e8b86af8
AD
902 throw std::runtime_error (m);
903}
cff92661
AD
904
905int
906main (int argc, const char *argv[])
907{
25a6ad2f
AD
908 switch (argc)
909 {
910 case 2:
911 input = argv[1];
912 break;
913 case 3:
2c08dc50 914 assert (std::string(argv[1]) == "--debug");
25a6ad2f
AD
915 debug = 1;
916 input = argv[2];
917 break;
918 default:
919 abort ();
920 }
921
cff92661 922 yy::parser parser;
25a6ad2f 923 debug |= !!getenv ("YYDEBUG");
cff92661
AD
924 parser.set_debug_level (debug);
925 int res = 2;
926 try
927 {
928 res = parser.parse ();
929 }
930 catch (const std::exception& e)
931 {
932 std::cerr << "exception caught: " << e.what () << std::endl;
933 }
934 catch (...)
935 {
936 std::cerr << "unknown exception caught" << std::endl;
937 }
8d0b7cef 938 Object::log (YY_NULLPTR, "end");
e8b86af8 939 assert (Object::empty());
cff92661
AD
940 return res;
941}
942]])
e8b86af8 943AT_BISON_CHECK([[-o input.cc --report=all input.yy]])
be6fa942 944AT_COMPILE_CXX([[input]])
cff92661
AD
945
946AT_PARSER_CHECK([[./input aaaas]], [[2]], [[]],
a2642464 947[[exception caught: reduction
cff92661
AD
948]])
949
950AT_PARSER_CHECK([[./input aaaal]], [[2]], [[]],
a2642464
AD
951[[exception caught: yylex
952]])
953
954AT_PARSER_CHECK([[./input i]], [[2]], [[]],
955[[exception caught: initial-action
cff92661
AD
956]])
957
25a6ad2f
AD
958AT_PARSER_CHECK([[./input aaaap]])
959
960AT_PARSER_CHECK([[./input --debug aaaap]], [[2]], [[]], [[stderr]])
ebbc76d0 961AT_CHECK([[grep '^exception caught: printer$' stderr]], [], [ignore])
25a6ad2f 962
e8b86af8
AD
963AT_PARSER_CHECK([[./input aaaae]], [[2]], [[]],
964[[exception caught: syntax error
965]])
966
967AT_PARSER_CHECK([[./input aaaaE]], [[2]], [[]],
968[[exception caught: syntax error, unexpected $end, expecting 'a'
969]])
970
971AT_PARSER_CHECK([[./input aaaaT]], [[1]])
972
5cf6e669 973AT_PARSER_CHECK([[./input aaaaR]], [m4_if([$2], [with], [0], [1])])
e8b86af8 974
cff92661
AD
975AT_BISON_OPTION_POPDEFS
976
977AT_CLEANUP
dc8e535c
AD
978])
979
5cf6e669
AD
980AT_TEST([], [with])
981AT_TEST([], [without])
982AT_TEST([%define api.value.type variant], [with])
983AT_TEST([%define api.value.type variant], [without])
dc8e535c
AD
984
985m4_popdef([AT_TEST])
99d795e8 986
e1d06b52
AD
987## ------------------------------------- ##
988## C++ GLR parser identifier shadowing. ##
989## ------------------------------------- ##
99d795e8
TR
990
991AT_SETUP([[C++ GLR parser identifier shadowing]])
992
993AT_DATA_GRAMMAR([input.yy], [
994%skeleton "glr.cc"
995
996%union
997{
998 int ival;
999}
1000
1001%token <ival> ZERO;
1002
1003%code
1004{
1005 int yylex (yy::parser::semantic_type *yylval);
1006}
1007
1008%%
1009exp: ZERO
1010
1011%%
1012
1013int yylex (yy::parser::semantic_type *yylval)
1014{
32f4c0a1
TR
1015 // Note: this argument is unused, but named on purpose. There used to be a
1016 // bug with a macro that erroneously expanded this identifier to
1017 // yystackp->yyval.
1018 YYUSE (yylval);
99d795e8
TR
1019 return yy::parser::token::ZERO;
1020}
1021
32f4c0a1
TR
1022void yy::parser::error (std::string const&)
1023{}
99d795e8 1024
e1d06b52 1025int main ()
99d795e8
TR
1026{}
1027])
1028
1029AT_BISON_CHECK([[-o input.cc input.yy]])
1030AT_COMPILE_CXX([[input]])
1031
1032AT_CLEANUP